#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<list>
#include "QueryProjector.h"
#include "SplitDeclaration.h"

using namespace std;

QueryProjector::QueryProjector() {
}

void QueryProjector::run()
{
	string filename, query, declaration;
	STATUS status;
	COLLECTION varlist;
	vector<std::string> output;
	int isValidQuery;

	//cout<<"\nEnter File Name which contains the Simple Language Code";
	//getline(cin,filename);

	VarTable *v = VarTable::getInstance();
	StatTable *s = StatTable::getInstance();

	// Call the declaration add function
	SplitDeclaration SpDc; 
	QueryEvaluator obj;
	QRYSTATEMENT_TYPE asstType;
	list<string> result;

	do{
		cout<<"Enter Declarations: ";
		getline(cin,declaration);

		asstType=SpDc.splitDeclaration(obj, declaration,varlist);

		if(asstType ==_INVALID)
			cout<<"\n\t[!] Invalid Declarations.\n\n";

	} while(asstType==_INVALID);
	
	cout<<"\n\t[!] Enter exit to return to menu.\n\n";

	//For the Query
	do{
		cout<<"\nEnter Query: ";
		getline(cin,query);

		boost::trim(query);

		// Call the Query Evaluator
		
		if(!query.compare("exit")==0)
		{

			if(!query.empty())
				isValidQuery = obj.validateQuery(query, result);
			else isValidQuery = -1;

			if(isValidQuery!=-1 && isValidQuery!=-2)
			{
				obj.executeQuery(result);
				
				//Print the output- list of integers saved in outputs.
				cout<<"\nOutput is as follows:\n";
				while(!result.empty() && result.size()!=1)
				{
					cout<<result.front();
					result.pop_front();
					cout<<", ";
				}
				if(!result.empty())
					cout<<result.front()<<"\n";
				
			}

			result.clear();

		}

	} while(!(query.compare("exit")==0));
	cout << "\n\n" << endl;
}

